package top.lyjwn.ajui.activity;

import android.os.Bundle;
import android.util.Log;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import top.lyjwn.ajui.R;
import top.lyjwn.ajui.base.BaseActivity;
import top.lyjwn.ajui.data.User;
import top.lyjwn.ajui.listener.OnCallBackListener;
import top.lyjwn.ajui.network.HttpUtil;
import top.lyjwn.ajui.network.OnCallback;
import top.lyjwn.ajui.test.ChatListAdapter;
import top.lyjwn.ajui.util.LogUtil;
import top.lyjwn.ajui.util.UrlUtil;
import top.lyjwn.ajui.widget.list.ListView;

public class ChatListActivity extends BaseActivity {

    private ListView chat_list_view;

    private ChatListAdapter chatListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        chat_list_view = findViewById(R.id.chat_list_view);
         chatListAdapter = new ChatListAdapter();
        chat_list_view.setAdapter(chatListAdapter);
        chat_list_view.setOnRefreshListener(new ListView.OnRefreshListener() {
            @Override
            public void onRefresh(SmartRefreshLayout refreshLayout) {
                loadData(new OnCallBackListener() {
                    @Override
                    public void finish(boolean is, Object o) {
                        refreshLayout.finishRefresh();
                    }
                });
            }
        });
        loadData(new OnCallBackListener() {
            @Override
            public void finish(boolean is, Object o) {

            }
        });
    }

   private void loadData(OnCallBackListener onCallback){
        HttpUtil.get(UrlUtil.HOST +"/chat/get/online/user/list", new OnCallback() {
            @Override
            public void onResponse(boolean done, Integer code, String msg, String data) {
                if (done){
                    LogUtil.d("聊天列表",data);
                    JSONObject jsonObject = JSONObject.parseObject(data);
                    JSONArray list = jsonObject.getJSONArray("list");
                    List<User> users = list.toJavaList(User.class);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            ChatListActivity.this.chatListAdapter.refresh(users);
                            onCallback.success(msg);
                        }
                    });
                }else{
                    LogUtil.e("聊天列表",data);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            onCallback.fail(msg);
                        }
                    });
                }

            }
        });
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_activity_chat_list;
    }
}
